﻿using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace ft_module_parser
{
     class pv_lists
    {
        public List<pv_list> lists = new List<pv_list>();
        public void ParseFT(string filename)
        {
            using (StreamReader file = new StreamReader(filename))
            {
                while (file.Peek() >= 0)
                {
                    string line = file.ReadLine();
                    string[] line2 = line.Split('.');
                    int index = int.Parse(line2[1]);

                    var curPv = lists.Where(c => c.index == index).FirstOrDefault();
                    if (curPv == null)
                    {
                        lists.Add(new pv_list(index));
                        curPv = lists.Where(c => c.index == index).FirstOrDefault();
                    }
                    string[] line3 = line2[2].Split('=');
                    if (line3[0] == "name")
                    {
                        curPv.name = line3[1];
                    }

                    if (line3[0] == "id")
                    {
                        curPv.id = int.Parse(line3[1]);
                    }

                    if (line3[0] == "ignore")
                    {
                        curPv.ignore = int.Parse(line3[1]);
                    }

                    pv_listdiff curDiff = null;
                    if (line2[2] == "easy")
                    {
                        var type = 0;
                        curDiff = curPv.difficulties.Where(c => c.type == type).FirstOrDefault();
                        if (curDiff == null)
                        {
                            curPv.difficulties.Add(new pv_listdiff(type));
                            curDiff = curPv.difficulties.Where(c => c.type == type).FirstOrDefault();
                        }
                    }
                    else
                    if (line2[2] == "normal")
                    {
                        var type = 1;
                        curDiff = curPv.difficulties.Where(c => c.type == type).FirstOrDefault();
                        if (curDiff == null)
                        {
                            curPv.difficulties.Add(new pv_listdiff(type));
                            curDiff = curPv.difficulties.Where(c => c.type == type).FirstOrDefault();
                        }
                    }
                    else
                    if (line2[2] == "hard")
                    {
                        var type = 2;
                        curDiff = curPv.difficulties.Where(c => c.type == type).FirstOrDefault();
                        if (curDiff == null)
                        {
                            curPv.difficulties.Add(new pv_listdiff(type));
                            curDiff = curPv.difficulties.Where(c => c.type == type).FirstOrDefault();
                        }
                    }
                    else
                    if (line2[2] == "extreme")
                    {
                        var type = 3;
                        curDiff = curPv.difficulties.Where(c => c.type == type).FirstOrDefault();
                        if (curDiff == null)
                        {
                            curPv.difficulties.Add(new pv_listdiff(type));
                            curDiff = curPv.difficulties.Where(c => c.type == type).FirstOrDefault();
                        }
                    }
                    else
                    if (line2[2] == "encore")
                    {
                        var type = 4;
                        curDiff = curPv.difficulties.Where(c => c.type == type).FirstOrDefault();
                        if (curPv == null)
                        {
                            curPv.difficulties.Add(new pv_listdiff(type));
                            curDiff = curPv.difficulties.Where(c => c.type == type).FirstOrDefault();
                        }
                    } else
                    {
                        string[] linen = line2[2].Split('_');
                        if (linen[0] == "adv")
                        {
                            var type = 5;
                            curDiff = curPv.difficulties.Where(c => c.type == type).FirstOrDefault();
                            if (curDiff == null)
                            {
                                curPv.difficulties.Add(new pv_listdiff(type));
                                curDiff = curPv.difficulties.Where(c => c.type == type).FirstOrDefault();
                            }
                        }
                    }
                    
                    if (curDiff != null)
                    {
                        string[] line5;
                        try
                        {
                            line5 = line2[4].Split('=');
                        }
                        catch
                        {
                            line5 = line2[2].Split('=');
                        }
                        if (line5[0].Contains("ed_day"))
                            curDiff.ed_day = int.Parse(line5[1]);

                        if (line5[0].Contains("ed_month"))
                            curDiff.ed_month = int.Parse(line5[1]);

                        if (line5[0].Contains("ed_year"))
                            curDiff.ed_year = int.Parse(line5[1]);
                        
                        if (line5[0].Contains("st_day"))
                            curDiff.st_day = int.Parse(line5[1]);

                        if (line5[0].Contains("st_month"))
                            curDiff.st_month = int.Parse(line5[1]);

                        if (line5[0].Contains("st_year"))
                            curDiff.st_year = int.Parse(line5[1]);

                        if (line5[0].Contains("ver"))
                            curDiff.ver = int.Parse(line5[1]);
                    }
                }
            }
        }

        public void ParseA(string filename)
        {
           
        }

         public void SaveToA(string filename)
        {
            using (StreamWriter sw = new StreamWriter(filename))
            {
                foreach (var pv in lists)
                {
                    string header = "pv_list." + pv.index + ".";
                    
                    foreach (var i in pv.difficulties)
                    {
                        
                        sw.WriteLine(header + i.getDifficulty() + "_" + "ed_day=" + i.ed_day);
                        sw.WriteLine(header + i.getDifficulty() + "_" + "ed_month=" + i.ed_month);
                        sw.WriteLine(header + i.getDifficulty() + "_" + "ed_year=" + i.ed_year);
                        sw.WriteLine(header + i.getDifficulty() + "_" + "st_day=" + i.st_day);
                        sw.WriteLine(header + i.getDifficulty() + "_" + "st_month=" + i.st_month);
                        sw.WriteLine(header + i.getDifficulty() + "_" + "st_year=" + i.st_year);
                        if (i.ver != -2)
                        {
                            sw.WriteLine(header + i.getDifficulty() + "_" + "ver=" + i.ver);
                        }

                        if (i.getDifficulty() == "hard")
                        {
                            sw.WriteLine(header + "id=" + pv.id);
                            sw.WriteLine(header + "ignore=" + pv.ignore);
                            sw.WriteLine(header + "name=" + pv.name);
                        }
                    }
                }
                int max = lists.Max(i => i.index) + 1;
                sw.WriteLine("pv_list.data_list.length=" + max);
            }
        }

        public void SaveToJson()
        {
            JsonSerializer serializer = new JsonSerializer();
            using (StreamWriter sw = new StreamWriter(@"json_pvlist.txt"))
            using (JsonWriter writer = new JsonTextWriter(sw))
            {
                serializer.Serialize(writer, this);
            }
        }
    }
}
